import 'dart:io';

import 'package:tx_live_plugin_example/bean/user_model.dart';
import 'package:tx_live_plugin_example/http/url_path.dart';
import 'package:tx_live_plugin_example/main.dart';

///appType 401-公证云专业版
// String get appType => "30";//30为公证云
///手机设备类型
String get deviceType => Platform.isIOS ? "6" : "8";
///app的名称
String get appName => "";

///app版本名称
String get appVersion => "";

String get imei => '';

///咨询类型
const int MEETING_TYPE_CONSULT = 1;

///接谈类型
const int MEETING_TYPE_INTERVIEW = 2;

///通用代码抽取
class LogicPresenter {

  static UserModel? user;
  static void saveUserInfo(UserModel? user){
    LogicPresenter.user = user;
  }

  ///将值转为 int，值为空或转换失败，返回0
  static int parseInt(dynamic num){
    if(num == null) return 0;
    try{
      return int.parse("$num");
    }catch (e){
    }
    return 0;
  }


  ///url添加token等默认参数
  static String addToken(String url, {Map<String, dynamic>? data}) {
    //域名
    if (!url.contains("://")) {
      url = "$h5Url$url";
    }
    //参数
    if (!url.contains("?")) {
      url = "$url?";
    }
    StringBuffer sb = StringBuffer(url);
    Map<String, dynamic> para = Map();
    if (data != null) {
      para.addAll(data);
    }
    //添加默认参数
    para.addAll(LogicPresenter.getDefaultParams());
    para.forEach((key, value) {
      if (value != null) sb.write("&$key=$value");
    });

    return sb.toString();
  }
  ///获取当前时间，格式为yyyyMMddHHmmss
  static String currentTime([String pattern = "yyyyMMddHHmmss"]) {
    // return DateFormat(pattern).format(DateTime.now());
    var time = DateTime.now();
    String year = fourDigits(time.year);
    String month = twoDigits(time.month);
    String day = twoDigits(time.day);
    String hour = twoDigits(time.hour);
    String minute = twoDigits(time.minute);
    String second = twoDigits(time.second);
    return "$year$month$day$hour$minute$second";
  }

  static String fourDigits(int n) {
    int absN = n.abs();
    String sign = n < 0 ? "-" : "";
    if (absN >= 1000) return "$n";
    if (absN >= 100) return "${sign}0$absN";
    if (absN >= 10) return "${sign}00$absN";
    return "${sign}000$absN";
  }

  static String twoDigits(int n) {
    int absN = n.abs();
    if (absN >= 10) return "$absN";
    return "0$absN";
  }

  ///app接口默认参数
  static Map<String, dynamic> getDefaultParams() {
    Map<String, dynamic> map = {
      "appVersion": appVersion,
      "signTime": currentTime(),
      "imei": imei,
      "appType": LoginModel.userLogin?.getUserInfo()['appType'],
      "deviceType": deviceType,
      // "phoneModel": Build.MODEL
      // "systemVersion": Build.VERSION.RELEASE
    };
    final user = LogicPresenter.user;
    if (user != null) {
      map['token'] = user.token;
      map['userId'] = user.userId;
      map['orgId'] = user.orgId;
      // print("map token ${map['token']} ${user.token};"
      //     "map userId ${map['userId']} ${user.userId};"
      //     "map orgId ${map['orgId']} ${user.orgId};");
    }
    if (user?.notaryId != null) {
      map['notaryId'] = user?.notaryId;
    }
    return map;
  }
}
